package com.culturalCenter.dataCenter.service.impl.passengerflow;

import com.alibaba.fastjson.JSONObject;
import com.culturalCenter.dataCenter.client.SdkClient;
import com.culturalCenter.dataCenter.customEntity.responseParam.passengerflow.PassengerFlowLoginInfoResponse;
import com.culturalCenter.dataCenter.service.passengerflow.PassengerFlowLoginService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zhao
 * @Date 2020/12/14 11:06
 */
@Service
public class PassengerFlowLoginServiceImpl implements PassengerFlowLoginService {

    /**
     * 客流系统登录IP
     */
    @Value("${passengerFlow.ip}")
    private String ip;
    /**
     * 客流系统登录端口
     */
    @Value("${passengerFlow.port}")
    private String port;

    @Override
    public PassengerFlowLoginInfoResponse getLoginReturnInfo(String userName, String password) throws Exception {
        PassengerFlowLoginInfoResponse passengerFlowLoginInfoResponse = new PassengerFlowLoginInfoResponse();
        Map<String, String> headMap = new HashMap<>();
        headMap.put("Content-Type", "application/json;charset=UTF-8");
        String info = SdkClient.login(ip, port, userName, password, headMap);
        JSONObject jsonObject = JSONObject.parseObject(info);
        if (!jsonObject.isEmpty()) {
            passengerFlowLoginInfoResponse.setSuccess(jsonObject.getString("success"));
            passengerFlowLoginInfoResponse.setLoginName(jsonObject.getString("loginName"));
            passengerFlowLoginInfoResponse.setErrMsg(jsonObject.getString("errMsg"));
            passengerFlowLoginInfoResponse.setToken(jsonObject.getString("token"));
            passengerFlowLoginInfoResponse.setId(jsonObject.getString("id"));
            passengerFlowLoginInfoResponse.setCmsIp(jsonObject.getString("cmsIp"));
            passengerFlowLoginInfoResponse.setCmsPort(jsonObject.getString("cmsPort"));
            passengerFlowLoginInfoResponse.setOrgCode(jsonObject.getString("orgCode"));
            passengerFlowLoginInfoResponse.setPublicKey(jsonObject.getString("publicKey"));
            passengerFlowLoginInfoResponse.setNonce(jsonObject.getString("nonce"));
        }

        return passengerFlowLoginInfoResponse;
    }

}
